package logic

import (
	"context"
	"errors"
	"shuqi-novel/models"
	"shuqi-novel/novelrpc/internal/svc"
	"shuqi-novel/novelrpc/novelrpc"

	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type BookUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewBookUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BookUpdateLogic {
	return &BookUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 小说更新
func (l *BookUpdateLogic) BookUpdate(in *novelrpc.BookUpdateRequest) (*novelrpc.BookUpdateResponse, error) {
	// todo: add your logic here and delete this line

	var book models.Book
	models.DB.Debug().Where("id = ?", in.BookId).Find(&book)
	if book.ID == 0 {
		return nil, errors.New("当前小说不存在,修改失败")
	}

	book = models.Book{
		Model:  gorm.Model{ID: uint(in.BookId)},
		Title:  in.Title,
		Image:  in.Image,
		Desc:   in.Desc,
		UserId: in.UserId,
	}
	models.DB.Debug().Updates(&book)

	return &novelrpc.BookUpdateResponse{
		BookId: int64(book.ID),
	}, nil
}
